// JavaScript Document

$(document).ready(function() {
	
	//初始化掌柜名
	for(var i = 0; i < Seller.length; i++) {
		var newOption = new Option(Seller[i].sellerNick, Seller[i].sellerNick);
		$("#oneKey-sellerNick").append(newOption);
	}
		
	/**
	选择掌柜名
	*/
	$("#oneKey-sellerNick").change(function() {
        var value = $("#oneKey-sellerNick").val();
		validSellerNickOneKey(value);
    });
	
	/*
	检测搜索关键词
	*/
	$("#oneKey-search-words").keyup(function() {
		var value = $.trim($("#oneKey-search-words").val());
		validSearchWordsOneKey(value);
    });
	$("#oneKey-search-words").focusout(function() {
		var value = $.trim($("#oneKey-search-words").val());
		validSearchWordsOneKey(value);
	});
	
	/*
	检测宝贝链接
	*/
	$("#oneKey-pro-link").keyup(function() {
		var value = $.trim($("#oneKey-pro-link").val());
		if(value.length > 20) {
			validProLinkOneKey(value);
		}
    });
	$("#oneKey-pro-link").focusout(function() {
		var value = $.trim($("#oneKey-pro-link").val());
		validProLinkOneKey(value);
	});
	
	/*
	检测宝贝担保价
	*/
	$("#oneKey-price").keyup(function() {
		calFeibi();
    });
	function calFeibi() {
		var value = $.trim($("#oneKey-price").val());
		if(!validPriceOneKey(value)) {
			return;
		}
		//计算金额、飞币
		var params = getParamsOneKey();
		AQ.callAQ("caculateFeibi.do", params, function(result) {
			var price = $("#oneKey-price").val();
			$("#oneKey-sumMoney").text(price);
			$("#oneKey-sumFeibi").text(result[0].info);
		}, function() {});
	}
	
	/**
	点击发布任务按钮
	*/
	$("#oneKey-publish").click(function() {
		if(validParamsOneKey()) {
			publishTaskOneKey();
		}
    });
	
	
});	//$(document).ready结束



/*
检测掌柜名
*/
function validSellerNickOneKey(value) {
	var value = $("#oneKey-sellerNick").val();
	if(value != 0) {
		$("#oneKey-sellerNick").css("border-color", "#DDDDDD");
		$("#oneKey-sellerNick-error").text("");
		return true;
	} else {
		$("#oneKey-sellerNick").css("border-color", "#F00000");
		$("#oneKey-sellerNick-error").text("× 请选择掌柜名");
		return false;
	}
}

/*
检测搜索关键词
*/
function validSearchWordsOneKey(value) {
	var reg = /^(.{1,60})$/;
 	var result = reg.test(value);
    if(result) {
		$("#oneKey-search-words").css("border-color", "#DDDDDD");
		$("#oneKey-search-words-error").text("");
		return true;
	} else {
		$("#oneKey-search-words").css("border-color", "#F00000");
		$("#oneKey-search-words-error").text("× 请输入有效的搜索关键词");
		return false;
	}
}

/*
检测宝贝链接
*/
function validProLinkOneKey(value) {
	var reg = /.{1,50}\.(taobao\.com)|(tmall\.com)\/.{1,220}id=[0-9]{8,20}/;
 	var result = reg.test(value);
    if(result) {
		$("#oneKey-pro-link").css("border-color", "#DDDDDD");
		$("#oneKey-pro-link-error").text("");
		return true;
	} else {
		$("#oneKey-pro-link").css("border-color", "#F00000");
		$("#oneKey-pro-link-error").text("× 请输入有效的宝贝链接");
		return false;
	}
}

/*
检测宝贝担保价
*/
function validPriceOneKey(value) {
	var reg =  /^(([0-9]{1,4}\.[0-9]{0,2})|([0-9]{1,4}))$/;
 	var result = reg.test(value);
    if(result) {
		$("#oneKey-price").css("border-color", "#DDDDDD");
		$("#oneKey-price-error").text("");
		return true;
	} else {
		$("#oneKey-price").css("border-color", "#F00000");
		$("#oneKey-price-error").text("× 请输入有效的宝贝担保价");
		return false;
	}
}

/*
检测所有参数
*/
function validParamsOneKey() {
		var check_sellerNick = validSellerNickOneKey($.trim($("#oneKey-sellerNick").val()));
		var check_search = validSearchWordsOneKey($.trim($("#oneKey-search-words").val()));
		var check_link = validProLinkOneKey($.trim($("#oneKey-pro-link").val()));
		var check_price = validPriceOneKey($.trim($("#oneKey-price").val()));
		//必填参数都正确
		if(check_sellerNick && check_search && check_link && check_price) {
			return true;
		} else {
			return false;	
		}
}

/**
获取参数
*/
function getParamsOneKey() {
	var sellerNick = $("#oneKey-sellerNick").val();
	var	searchWord = $("#oneKey-search-words").val();
	var productLink = $("#oneKey-pro-link").val();
	var price = $("#oneKey-price").val();
	
	var params = {
		orderType: 1,
		sellerNick: sellerNick,
		searchType: 1,
		productLink: productLink,
		price: price,
		modifyPrice: 0,
		amount: 1,
		refundType: 1,
		searchWord: searchWord,
		searchTips: "",
		imageTips: "",
		rateTime: 8,
		noWordRate: 0,
		phoneOrder: 0,
		checkBuyerIp: 1,
		checkTaker: 1,
		realName: 1,
		rankLimit: 12,
		takeArea: "",
		compare: 3,
		browseFoot: 1,
		collect: 1,
		browseOthers: 3,
		talk: 0,
		stayTime: 5,
		adress: "",
		rateContent: "",
		ratePic1: "",
		ratePic2: "",
		ratePic3: "",
		ratePic4: "",
		ratePic5: "",
		rateSuccessPic: 0,
		tellTaker: "",
		addFeibi: 0,
		taskNum: 1,
		interval: 0,
	};
	return params;
}

/*
发布任务（提交数据）
*/
function publishTaskOneKey() {
	var params = getParamsOneKey();
	AQ.callAQ("publishTask.do", params, function(result) {
		Utils.checkDialog("放单成功", "查看已发任务", "返回", function() {
			location.replace("published-task.html");
		}, function() {});
	}, function(err, code) {
		Utils.msgDialog(err + "["+ code +"]");
	});
}
